//
//  NSMutableAttributedString+FCReplaceString.m
//  辅城
//
//  Created by WJC on 2018/6/21.
//  Copyright © 2018年 吴 剑川. All rights reserved.
//

#import "NSMutableAttributedString+FCReplaceString.h"

@implementation NSMutableAttributedString (FCReplaceString)
-(NSMutableAttributedString *)replaceColorWithStr:(NSString *)str andColor:(UIColor *)color{
    NSMutableString *string=[self.string mutableCopy];
     NSRange range = [string rangeOfString:str];
    while (range.location||range.length>0) {
        range = [string rangeOfString:str];
        if (range.location != NSNotFound) {
          [self addAttribute:NSForegroundColorAttributeName value:color range:range];
            for (int i=0; i<str.length; i++) {
                 [string replaceCharactersInRange:NSMakeRange(range.location+i, 1) withString:@"ㄆ"];
            }
        }else{
            break;
        }
    }
    
    return self;
}
-(NSMutableAttributedString *)replaceBackColorWithStr:(NSString *)str andColor:(UIColor *)color{
    NSMutableString *string=[self.string mutableCopy];
     NSRange range = [string rangeOfString:str];
    while (range.location||range.length>0) {
        range = [string rangeOfString:str];
        if (range.location != NSNotFound) {
          [self addAttribute:NSBackgroundColorAttributeName value:color range:range];
            for (int i=0; i<str.length; i++) {
                 [string replaceCharactersInRange:NSMakeRange(range.location+i, 1) withString:@"ㄆ"];
            }
        }else{
            break;
        }
    }
    
    return self;
}
-(NSMutableAttributedString *)replaceFontWithStr:(NSString *)str andColor:(UIFont *)font{
    if (!font) {
        font = [UIFont systemFontOfSize:15];
    }
    NSMutableString *string=[self.string mutableCopy];
    NSRange range = [string rangeOfString:str];
    while (range.location) {
        range = [string rangeOfString:str];
        if (range.location != NSNotFound) {
            [self addAttribute:NSFontAttributeName value:font range:range];
            for (int i=0; i<str.length; i++) {
                [string replaceCharactersInRange:NSMakeRange(range.location+i, 1) withString:@"ㄆ"];
            }
        }else{
            break;
        }
    }
    
    return self;
}
-(NSMutableAttributedString *)setLinespaceWithHeight:(CGFloat)height{
//    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        
        [paragraphStyle setLineSpacing:height];//调整行间距
        
        [self addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.string.length)];
    return self;
}
-(NSMutableAttributedString *)addAttrWithImage:(UIImage*)image adnbounds:(CGRect)bounds{
        //创建图片
        NSTextAttachment *attach = [[NSTextAttachment alloc] init];
        attach.image =image; //设置图片
        attach.bounds = bounds; //设置图片大小、位置
        NSAttributedString *str2 = [NSAttributedString attributedStringWithAttachment:attach];
        
//        [self appendAttributedString:self];
        [self appendAttributedString:str2];
    return self;  //将富文本添加到lable上

}
@end
